package edu.corn.statemachine.core.builder;


import edu.corn.statemachine.core.Action;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;

/**
 * 用于构造一例状态流转事件。
 * 没有携带其他时间。这样似乎对修改是封闭的了。
 *
 * @param <S> 状态。
 * @param <E> 事件。
 */
@Getter
public class Trigger<S extends Enum<S>, E extends Enum<E>> {


    /**
     * 源状态。
     */
    private final S sourceState;

    /**
     * 目标状态。
     */
    private final S targetState;

    /**
     * 触发事件。
     */
    private final E eventType;

    /**
     * 触发行为。
     */
    private final Action<S, E> action;

    // 私有构造函数，只能通过Builder创建
    private Trigger(Builder<S, E> builder) {
        this.sourceState = builder.sourceState;
        this.targetState = builder.targetState;
        this.eventType = builder.eventType;
        this.action = builder.action;
    }


    // 静态内部类，用于构建TriggerBuilder实例
    public static class Builder<S extends Enum<S>, E extends Enum<E>> implements ITriggerBuilder<S, E> {
        private S sourceState;
        private S targetState;
        private E eventType;
        private Action<S, E> action;
        AbstractStateMachineBuilder<S,E> builder;

        public Builder(AbstractStateMachineBuilder<S,E> builder) {
            this.builder = builder;
        }

        public Builder<S, E> sourceState(S sourceState) {
            this.sourceState = sourceState;
            return this;
        }

        public Builder<S, E> targetState(@NotNull S targetState) {
            this.targetState = targetState;
            return this;
        }

        public Builder<S, E> eventType(@NotNull E eventType) {
            this.eventType = eventType;
            return this;
        }

        public Builder<S, E> action(Action<S, E> action) {
            this.action = action;
            return this;
        }

        @Override
        public Trigger<S, E> build() {
            return new Trigger<>(this);
        }

        @Override
        public AbstractStateMachineBuilder<S, E> done() {
            return builder;
        }
    }
}

